home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 112 (1989-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 112 (1989-11-15)(Ossowski, Stefan)(DE)(PD).adf / C64Emul / src / screen.c < prev    next >
C/C++ Source or Header  |  1989-07-02  |  8KB  |  391 lines

  1. /*
  2.  *        Commodore 64 Spoof Emulator (C) Eddy Carroll, 1st April 1988
  3.  *
  4.  * Module: SCREEN.C
  5.  *
  6.  * This module contains the various screen handling routines used by
  7.  * the emulator.
  8.  *
  9.  */
  10.  
  11. #define  SCREEN
  12.  
  13. #include <exec/types.h>
  14. #include <exec/io.h>
  15. #include <intuition/intuition.h>
  16. #include <proto/exec.h>
  17. #include <proto/graphics.h>
  18. #include <proto/intuition.h>
  19. #include "screen.h"
  20.  
  21. struct RastPort *rp;
  22. int openconsole = 0;
  23.  
  24. struct NewScreen NewScreen = {
  25.     0,0,320,200,    /* Screen position & size     */
  26.     2,                /* 2 bitplanes = 4 colours     */
  27.     0,1,            /* Default text colours        */
  28.     0,                /* Lores, non-interlace        */
  29.     CUSTOMSCREEN,    /* Screen type                */
  30.     0,                /* Use default font for now    */
  31.     "C64 Emulator",    /* Screen title                */
  32.     0,                /* No gadgets                 */
  33.     0                /* No custom bitmap            */
  34. };
  35.  
  36. #define NORMALFLAGS WINDOWCLOSE | WINDOWSIZING | WINDOWDEPTH | WINDOWDRAG
  37. struct NewWindow NewWindow = {
  38.     0,0,320,200,      /* Window position & Size    */
  39.     0,1,            /* Default text colours        */
  40.     MENUPICK      |    /* Ok, we want to know         */
  41.     INTUITICKS,        /* about menus & timings     */
  42.     BACKDROP      |    /* These four flags define    */
  43.     BORDERLESS    |    /* the type of window we    */
  44.     ACTIVATE      |    /* open. Here, we want an    */
  45.     0       ,        /* "invisible" window.        */
  46.     NULL,            /* No gadgets                */
  47.     NULL,            /* Default menus check mark    */
  48.     NULL,              /* No window title            */
  49.     NULL,            /* Will point to our screen    */
  50.     NULL,            /* No special bitmap used    */
  51.     0,0,320,200,    /* No resizing so not used  */
  52.     CUSTOMSCREEN     /* Open this on OUR screen    */
  53. };
  54.  
  55. UWORD blackcol[4] = {    /* All black array for initial screen   */
  56.     0,0,0,0};
  57.  
  58. UWORD screencol[4] = {    /* Default colours for custom screen    */
  59.     0x077E,                /* Colour 0 = Light blue                */
  60.     0x0EEE,                /* Colour 1 = White                        */
  61.     0x077E,                /* Colour 2 = Lighter blue                */
  62.     0x011C                 /* Colour 3 = Dark Blue                    */
  63. };
  64.  
  65. struct IntuiText mytext[] = {
  66. /*  Pens   Mode   Pos   Font    Text                       Next */
  67. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" About...",       NULL},
  68. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" Show Title",     NULL},
  69. {   0,1,   JAM1,  0,0,  NULL,   (UBYTE *)" Quit",           NULL}};
  70.  
  71.  
  72.  
  73. #define MENUFLAGS ITEMTEXT | ITEMENABLED | HIGHCOMP
  74. #define IDATA(off,str,key) 0,off,110,9,MENUFLAGS,0,str,NULL,key,NULL,NULL
  75.  
  76. struct MenuItem myitems[] = {
  77. { &myitems[1],  IDATA( 0, (APTR)&mytext[0],  M_ABOUT)  },
  78. { &myitems[2],  IDATA(10, (APTR)&mytext[1],  M_TITLE)  },
  79. { NULL,         IDATA(20, (APTR)&mytext[2],  M_QUIT )  }};
  80.  
  81. struct Menu mymenus = {NULL, 0,0,100,10, MENUENABLED, "Project", myitems};
  82.  
  83.  
  84. /* 
  85.  * Free all resources allocated in this module
  86.  *
  87.  */
  88.  
  89. void cleanup(err)
  90. int err;
  91. {
  92.     if (openconsole)
  93.         CloseDevice(ConReadReq);
  94.  
  95.     if (ConReadReq)
  96.         DeleteStdIO(ConReadReq);
  97.  
  98.     if (ConReadPort)
  99.         DeletePort(ConReadPort);
  100.  
  101.     if (mywin) {
  102.         ClearMenuStrip(mywin);
  103.         CloseWindow(mywin);
  104.     }
  105.     if (myscreen)
  106.         CloseScreen(myscreen);
  107.  
  108.     if (IntuitionBase)
  109.         CloseLibrary(IntuitionBase);
  110.  
  111.     if (GfxBase)
  112.         CloseLibrary(GfxBase);
  113.  
  114.     exit(err);
  115. }
  116.  
  117.  
  118.  
  119. /* 
  120.  * Clears the screen
  121.  *
  122.  */
  123.  
  124. void clearscreen()
  125. {
  126.     register char *p;
  127.     register int i;
  128.     SetRast(rp,3);
  129.     for (p = (char *)screen, i = 1000; i--; *p++ = ' ')
  130.         ;
  131.     cursorx = 0;
  132.     cursory = 0;
  133. }
  134.  
  135.  
  136. /* 
  137.  * Tells console device we want to read a character from keyboard
  138.  *
  139.  */
  140.  
  141. void QueueRead(request,whereto)
  142. struct IOStdReq *request;
  143. char *whereto;
  144. {
  145.     request->io_Command    = CMD_READ;
  146.     request->io_Data    = (APTR)whereto;
  147.     request->io_Length    = 1;
  148.     SendIO(request);
  149. }
  150.  
  151. /*  
  152.  * Initialises the screen, character set, colours etc. Exits program with
  153.  * error code if an error occurs.
  154.  *
  155.  */
  156.  
  157. void initscreen()
  158. {
  159.     if (!mywin) {
  160.         if ((IntuitionBase = 
  161.             (struct IntuitionBase *)OpenLibrary("intuition.library",0)) == 0)
  162.             cleanup(1);
  163.  
  164.         if ((GfxBase =
  165.             (struct GfxBase *)OpenLibrary("graphics.library",0)) == 0)
  166.             cleanup(2);
  167.  
  168.         if ((myscreen = (struct Screen *)OpenScreen(&NewScreen)) == 0)
  169.             cleanup(3);
  170.  
  171.         LoadRGB4(&(myscreen->ViewPort),blackcol,4); 
  172.         ShowTitle(myscreen,0);
  173.         NewWindow.Screen = myscreen;
  174.  
  175.         if ((mywin =  (struct Window *)OpenWindow(&NewWindow)) == 0)
  176.             cleanup(4);
  177.  
  178.         SetMenuStrip(mywin, &mymenus);
  179.         rp = mywin->RPort;
  180.  
  181.         /* Initialise communication ports for Console */
  182.  
  183.         if ((ConReadPort = CreatePort(0,0)) == NULL)
  184.             cleanup(5);
  185.  
  186.         if ((ConReadReq = CreateStdIO(ConReadPort)) == NULL)
  187.             cleanup(6);
  188.  
  189.         ConReadReq->io_Data = (APTR)mywin;
  190.         ConReadReq->io_Length = sizeof(*mywin);
  191.  
  192.         /* Open console and tell it to turn off the cursor */
  193.         if (OpenDevice("console.device",0,ConReadReq,0))
  194.             cleanup(7);
  195.         openconsole = 1;
  196.  
  197.         /* Tell console to get first character ready for us */
  198.         QueueRead(ConReadReq,constring);
  199.  
  200.         SetAPen(rp,2);    /* Set primary colour */
  201.         SetBPen(rp,3);    /* Set secondary colour */
  202.         clearscreen();    /* Fill screen to raster colour */
  203.         LoadRGB4(&(myscreen->ViewPort),screencol,4); 
  204.  
  205.     }
  206. }
  207.  
  208.  
  209. /*
  210.  * Outputs a character to the screen at co-ordinates x,y. If mode is 0,
  211.  * then output normal character, else output reverse character.
  212.  *
  213.  */
  214.  
  215. void writechar(x,y,ch,mode)
  216. int x,y;
  217. char ch;
  218. int mode;
  219. {
  220.     char s[2];
  221.     s[0] = ch;
  222.     Move(rp,x * 8, y * 8 + 6); /* Quick multiply by 8 */
  223.     if (mode) {
  224.         SetDrMd(rp,INVERSVID|JAM2);
  225.         Text(rp,s,1);
  226.         SetDrMd(rp,          JAM2);
  227.     } else
  228.         Text(rp,s,1);
  229. }
  230.  
  231. /* 
  232.  * Scrolls the screen up one line. Both the visible rastport screen, 
  233.  * and the internal screen are scrolled.
  234.  *
  235.  */
  236.  
  237. void scrollup()
  238. {
  239.     register char *p, *q;
  240.     register int i;
  241.  
  242.     ScrollRaster(rp,0,8,0,0,319,199);
  243.     for (p = (char *)screen,q = (char *)screen+40, i = 960; i--; *p++ = *q++)
  244.         ;
  245.     for (i = 40; i--; *p++ = ' ')
  246.         ;
  247.     cursory--;
  248.  
  249. }
  250.  
  251.  
  252.  
  253. /*
  254.  * Handles special characters like CR, cursor keys etc.
  255.  *
  256.  */
  257.  
  258. void special(ch)
  259. char ch;
  260. {
  261.     register char *p, *q;
  262.     register int i,j;
  263.  
  264.     switch (ch) {
  265.  
  266.     case C_CR: /* Carriage Return */
  267.         cursorx = 0;
  268.         cursory++;
  269.         if (cursory >= 25)
  270.             scrollup();
  271.         break;
  272.  
  273.     case C_UP: /* Cursor Up */
  274.         if (cursory)
  275.             cursory--;
  276.         break;
  277.  
  278.     case C_DOWN: /* Cursor Down */
  279.         cursory++;
  280.         if (cursory >= 25)
  281.             scrollup();
  282.         break;
  283.  
  284.     case C_LEFT: /* Cursor Left */
  285.         if (cursorx == 0) {
  286.             if (cursory) {
  287.                 cursorx = 39;
  288.                 cursory--;
  289.             }
  290.         } else
  291.             cursorx--;
  292.         break;
  293.  
  294.     case C_RIGHT: /* Cursor Right */
  295.         cursorx++;
  296.         if (cursorx >= 40) {
  297.             cursorx = 0;
  298.             cursory++;
  299.             if (cursory >= 25)
  300.                 scrollup();
  301.         }
  302.         break;
  303.  
  304.     case C_DEL: /* Delete */
  305.         if (cursorx == 0) {
  306.             if (cursory) {
  307.                 cursorx = 39;
  308.                 cursory--;
  309.                 screen[cursory][cursorx] = ' ';
  310.                 writechar(cursorx,cursory,' ',0);
  311.             }
  312.         } else {
  313.             ScrollRaster(rp,8,0,cursorx*8,cursory*8,319,cursory*8+7);
  314.             /* Move chars from here to end of line back one space */
  315.             for (q = &(screen[cursory][cursorx]),
  316.                         p = q - 1, i = 40 - cursorx;
  317.                         i--;
  318.                         *p++ = *q++)
  319.                         ;
  320.             *p = ' ';
  321.             cursorx--;
  322.         }
  323.         break;
  324.  
  325.     case C_INSERT: /* Insert */
  326.         if (screen[cursory][39] == ' ' && cursorx < 39) {
  327.             ScrollRaster(rp,-8,0,cursorx*8,cursory*8,319,cursory*8+7);
  328.             for (p = &screen[cursory][39], q = p - 1, i = 39 - cursorx;
  329.                     i--; *p-- = *q--)
  330.                         ;
  331.             *p = ' ';
  332.         }
  333.         break;
  334.  
  335.     case C_CLEAR: /* Clear screen */
  336.         clearscreen();
  337.         break;
  338.  
  339.     case C_HOME: /* Home cursor */
  340.         cursorx = 0;
  341.         cursory = 0;
  342.         break;
  343.  
  344.     case C_REDRAW: /* Redraw entire screen */
  345.         for (i = 0; i < 25; i++)
  346.             for (j = 0; j < 40; j++)
  347.                 writechar(j,i,screen[i][j],0);
  348.         break;
  349.  
  350.     }
  351. }
  352.  
  353.  
  354.  
  355. /*
  356.  * Outputs character to screen at current cursor position, and advances
  357.  * cursor to next position. Scrolls screen if necessary.
  358.  *
  359.  */
  360.  
  361. void printchar(ch)
  362. char ch;
  363. {
  364.     if (ch < ' ')
  365.         special(ch);
  366.     else {
  367.         writechar(cursorx,cursory,ch,0);
  368.         screen[cursory][cursorx] = ch;
  369.         cursorx++;
  370.         if (cursorx >= 40) {
  371.             cursorx = 0;
  372.             cursory++;
  373.             if (cursory >= 25)
  374.                 scrollup();
  375.         }
  376.     }
  377. }
  378.  
  379.  
  380. /* 
  381.  * Outputs string to screen at current cursor position
  382.  *
  383.  */
  384.  
  385. void printmess(s)
  386. char *s;
  387. {
  388.     while (*s)
  389.         printchar(*s++);
  390. }
  391.